{ "cells": [ { "cell_type": "markdown", "id": "intro", "metadata": {}, "source": [ "# Combinations of Conditionals, Loops, and Collections in Python" ] }, { "cell_type": "markdown", "id": "concepto", "metadata": {}, "source": [ "## Step 1: Combining Conditionals, Loops, and Collections\n", "In Python, you can combine conditionals (`if`), loops (`for`, `while`), and collections (lists, dictionaries, etc.) to create powerful programs that solve complex problems. We will show you how this combination works in a simple way." ] }, { "cell_type": "markdown", "id": "lista-condicional", "metadata": {}, "source": [ "### Lists with Conditionals\n", "Imagine you have a list of numbers and you want to print only the even numbers. To do this, we use a `for` loop to iterate over the list, and inside the loop, we use an `if` statement to check if the number is even." ] }, { "cell_type": "code", "execution_count": null, "id": "codigo-lista-condicional", "metadata": {}, "outputs": [], "source": [ "numbers = [1, 2, 3, 4, 5, 6]\n", "for num in numbers:\n", " if num % 2 == 0: # Conditional to check if the number is even\n", " print(num) # Print the number if it is even" ] }, { "cell_type": "markdown", "id": "while-lista", "metadata": {}, "source": [ "### Lists with `while` Loops\n", "You can also use a `while` loop to iterate over a list. For example, if you want to count until the first number greater than 3 in a list, you can do it like this:" ] }, { "cell_type": "code", "execution_count": null, "id": "codigo-while-lista", "metadata": {}, "outputs": [], "source": [ "numbers = [1, 2, 3, 4, 5, 6]\n", "index = 0\n", "while numbers[index] <= 3:\n", " print(numbers[index])\n", " index += 1 # Increment index to move to the next number" ] }, { "cell_type": "markdown", "id": "diccionario-condicional", "metadata": {}, "source": [ "### Dictionaries with Conditionals\n", "In dictionaries, you can use a conditional to access specific values. For example, if you have a dictionary with people's ages, you can print only those people who are older than 18." ] }, { "cell_type": "code", "execution_count": null, "id": "codigo-diccionario-condicional", "metadata": {}, "outputs": [], "source": [ "people = {'Alice': 22, 'Bob': 17, 'Charlie': 20}\n", "for name, age in people.items():\n", " if age >= 18:\n", " print(f'{name} is over 18 years old.') # Prints only people older than 18" ] }, { "cell_type": "markdown", "id": "set-bucle", "metadata": {}, "source": [ "### Sets with Loops\n", "You can use a loop to iterate over a `set`, which is a collection of unique elements. For example, if you have a set of numbers and want to print only the odd numbers:" ] }, { "cell_type": "code", "execution_count": null, "id": "codigo-set-bucle", "metadata": {}, "outputs": [], "source": [ "unique_numbers = {1, 2, 3, 4, 5, 6}\n", "for num in unique_numbers:\n", " if num % 2 != 0: # Check if the number is odd\n", " print(num) # Print only odd numbers" ] }, { "cell_type": "markdown", "id": "combinacion-condicional-bucle-lista", "metadata": {}, "source": [ "### Combination of Conditionals, Loops, and Lists\n", "One very common way to use these combinations is to loop over a list with a `for` loop and apply a condition inside that loop to perform a specific action. For example, you can have a list of numbers and calculate the sum of only the positive numbers." ] }, { "cell_type": "code", "execution_count": null, "id": "codigo-combinacion-bucle-condicional", "metadata": {}, "outputs": [], "source": [ "numbers = [-1, 2, -3, 4, 5]\n", "sum_positive = 0\n", "for num in numbers:\n", " if num > 0: # Only sum positive numbers\n", " sum_positive += num\n", "print('The sum of positive numbers is:', sum_positive)" ] }, { "cell_type": "markdown", "id": "combinacion-condicional-bucle-diccionario", "metadata": {}, "source": [ "### Combination of Conditionals, Loops, and Dictionaries\n", "If you have a dictionary and want to apply a conditional to the values to perform an action, you can do it inside a loop. For example, let's say you have a dictionary with names and ages, and you want to print only those who are over 18 years old." ] }, { "cell_type": "code", "execution_count": null, "id": "codigo-diccionario-bucle-condicional", "metadata": {}, "outputs": [], "source": [ "people = {'Alice': 22, 'Bob': 17, 'Charlie': 20}\n", "for name, age in people.items():\n", " if age > 18: # Only print people over 18 years old\n", " print(f'{name} is over 18 years old.')" ] }, { "cell_type": "markdown", "id": "ejercicio", "metadata": {}, "source": [ "## Step 2: Practical Exercises\n", "Now that you've seen some examples, it's time to practice what you've learned." ] }, { "cell_type": "markdown", "id": "ejercicio-1", "metadata": {}, "source": [ "### Exercise 1: Sum of Positive Numbers\n", "Create a list of numbers that includes both positive and negative numbers. Then, use a `for` loop and a condition to sum only the positive numbers in the list. Print the result." ] }, { "cell_type": "markdown", "id": "ejercicio-2", "metadata": {}, "source": [ "### Exercise 2: Filter People Over 18 Years Old\n", "Create a dictionary with names and ages of people. Then, use a `for` loop and a condition to print only the people who are over 18 years old." ] }, { "cell_type": "markdown", "id": "ejercicio-3", "metadata": {}, "source": [ "### Exercise 3: Count Even Numbers in a List\n", "Create a list of numbers. Write a program that counts how many even numbers are in the list. Print the count of even numbers." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "name": "python", "version": "3.x" } }, "nbformat": 4, "nbformat_minor": 5 }